<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>template模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="iterator模式" href="iterator.html" />
    <link rel="prev" title="facade模式" href="facade.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="iterator.html" title="iterator模式"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="facade.html" title="facade模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="template">
<h1>template模式<a class="headerlink" href="#template" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-14"></span><span id="id2"></span><h2>template模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p><strong>The Template Method pattern</strong> defines the skeleton of an algorithm in a method,
deferring some steps to subclasses. <strong>Template Method</strong> lets subclasses redefine
certain steps of an algorithm without changing the algorithm&#8217;s structure.</p>
<p><strong>模板方法模式</strong> 在一个方法中定义了一个算法的框架(骨架), 把一些步骤推给子类.
<strong>模板方法</strong> 使得子类能够重新定义算法中的某些步骤而无须更改算法结构.</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>对于一个兼营咖啡和茶的店,两种饮品的烹饪方式是相同的:</p>
<ul class="simple">
<li>煮水</li>
<li>将饮品放入水中</li>
<li>倒入杯中</li>
<li>加入辅料</li>
</ul>
<p>可以使用 <strong>模板模式</strong> 来完成.</p>
<p>具体的实现类图如下:</p>
<img alt="../_images/template_beverage.png" src="../_images/template_beverage.png" />
<p>Template模式的类图如下:</p>
<img alt="../_images/template_class.png" src="../_images/template_class.png" />
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/template.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75</pre></td><td class="code"><div class="highlight"><pre><span class="cp">#include&lt;iostream&gt;</span>
<span class="cp">#include&lt;string&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">CaffeineBeverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">prepareRecipe</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">boildWater</span><span class="p">();</span>
            <span class="n">brew</span><span class="p">();</span>
            <span class="n">pourInCup</span><span class="p">();</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">customerWantsCondiments</span><span class="p">())</span>
            <span class="p">{</span>
                <span class="n">addCondiments</span><span class="p">();</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">boildWater</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Boiling water.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="kt">void</span> <span class="n">pourInCup</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Pouring into cup.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>

        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">brew</span><span class="p">(){}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">addCondiments</span><span class="p">(){}</span>

        <span class="k">virtual</span> <span class="kt">bool</span> <span class="n">customerWantsCondiments</span><span class="p">(){</span><span class="k">return</span> <span class="kc">true</span><span class="p">;}</span>    <span class="c">// the hook to offer a way to change the algorithm</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Coffee</span><span class="o">:</span><span class="k">public</span> <span class="n">CaffeineBeverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">brew</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Dripping Coffee through filter.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="kt">void</span> <span class="n">addCondiments</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Adding sugar and Milk&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>

        <span class="kt">bool</span> <span class="n">customerWantsCondiments</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="kt">char</span> <span class="n">choice</span><span class="p">;</span>
            <span class="k">while</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Whether you wanna add sugar and milk?(y/n)&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
                <span class="n">cin</span><span class="o">&gt;&gt;</span><span class="n">choice</span><span class="p">;</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">choice</span> <span class="o">==</span> <span class="sc">&#39;y&#39;</span> <span class="o">||</span> <span class="n">choice</span> <span class="o">==</span> <span class="sc">&#39;n&#39;</span><span class="p">)</span>
                    <span class="k">break</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">return</span> <span class="n">choice</span><span class="o">==</span><span class="sc">&#39;y&#39;</span><span class="o">?</span><span class="kc">true</span><span class="o">:</span><span class="kc">false</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Tea</span><span class="o">:</span><span class="k">public</span> <span class="n">CaffeineBeverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">brew</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Dripping Tea through filter.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="kt">void</span> <span class="n">addCondiments</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Adding lemon&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>

        <span class="kt">bool</span> <span class="n">customerWantsCondiments</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="kt">char</span> <span class="n">choice</span><span class="p">;</span>
            <span class="k">while</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Whether you wanna add lemon?(y/n)&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
                <span class="n">cin</span><span class="o">&gt;&gt;</span><span class="n">choice</span><span class="p">;</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">choice</span> <span class="o">==</span> <span class="sc">&#39;y&#39;</span> <span class="o">||</span> <span class="n">choice</span> <span class="o">==</span> <span class="sc">&#39;n&#39;</span><span class="p">)</span>
                    <span class="k">break</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">return</span> <span class="n">choice</span><span class="o">==</span><span class="sc">&#39;y&#39;</span><span class="o">?</span><span class="kc">true</span><span class="o">:</span><span class="kc">false</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">Coffee</span> <span class="n">coffee</span><span class="p">;</span>
    <span class="n">Tea</span> <span class="n">tea</span><span class="p">;</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;##### Preparing the Coffee ######&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">coffee</span><span class="p">.</span><span class="n">prepareRecipe</span><span class="p">();</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;##### Preparing the Tea ######&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">tea</span><span class="p">.</span><span class="n">prepareRecipe</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>##### Preparing the Coffee ######
Boiling water.
Dripping Coffee through filter.
Pouring into cup.
Whether you wanna add sugar and milk?(y/n)
y
Adding sugar and Milk
##### Preparing the Tea ######
Boiling water.
Dripping Tea through filter.
Pouring into cup.
Whether you wanna add lemon?(y/n)
n</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>如果需要更改算法具体的实现,则无须更改具体的算法类,只需要更改相应的实现即可.</p>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p>设计准则:</p>
<p><strong>The Hollywood Principle: Don&#8217;t call us, We&#8217;ll call you.</strong></p>
<p><a class="reference internal" href="#id1"><em>Template模式</em></a> vs. <a class="reference external" href="strategy.html#id1"><em>Strategy模式</em></a> vs. <a class="reference external" href="factory.html#factory"><em>Factory模式</em></a>:</p>
<ul class="simple">
<li><strong>Template模式</strong> 子类决定如何实现一个算法的步骤</li>
<li><strong>Strategy模式</strong> 封装了变化的行为并使用 <strong>委托</strong> 决定使用哪个行为</li>
<li><strong>Factory模式</strong> 子类决定哪个具体类实例化</li>
</ul>
<p>在 <strong>模板模式</strong> 中注意 <strong>hook</strong> 的使用(hook是一些方法,它们在抽象类默认无功能或者有默认功能,但可以在子类中重载)</p>
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Template_method_pattern">Template_Method_Pattern</a></li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">template模式</a><ul>
<li><a class="reference external" href="#id1">template模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="facade.html"
                                  title="上一章">facade模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="iterator.html"
                                  title="下一章">iterator模式</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/template.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="iterator.html" title="iterator模式"
             >下一页</a> |</li>
        <li class="right" >
          <a href="facade.html" title="facade模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>